package com.itcast.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.cloud.netflix.ribbon.apache.HttpClientUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//处理请求中的参数的封装和解析

public class HttpRequestHelper {
    public static Map<String, Object> switchMap(Map<String, String[]> paramMap) {
        Map<String, Object> resultMap = new HashMap<>();
        for (Map.Entry<String, String[]> param : paramMap.entrySet()) {
            resultMap.put(param.getKey(), param.getValue()[0]);
        }
        return resultMap;
    }

    public static Object getTimestamp() {

        return System.currentTimeMillis() / 1000;
    }


    public static JSONObject sendRequest(Map<String, Object> paramMap, String url) {
        //转换格式，将Map里的Object转为String
        Map<String, String> paramMap1 = new HashMap<>();
        for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
            paramMap1.put(entry.getKey(), entry.getValue().toString());
        }

		try {
            HttpResponse result = HttpUtils.doPost(url, null, "POST", new HashMap<>(), paramMap1, new HashMap<>());
            String s = EntityUtils.toString(result.getEntity());
            return JSONObject.parseObject(s);
        } catch (Exception e) {

			throw new RuntimeException(e);
		}


    }
}
